package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 新唐人
 * @author KK
 *
 */
public class DwnewsBBSAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.dwnews.com/.*/[0-9]{5,}.html",
		"http://.*.dwnews.com/threadshow.php\\?tid=[0-9]*",
		"http://.*.dwnews.com/news/[0-9]*-[0-9]*-[0-9]*/[0-9]*.html",
		"http://.*.dwnews.com/post-[0-9]*.html",
	};


	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		if (url.startsWith("http://forum.dwnews.com")){
			title =doc.select("h1 a").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div[id^=postcontent_]"));
			Element first=doc.select(".toptip").first();
			if (first!=null){
				date=DateUtils.matchDate(first.text());
			}
			Element first2=doc.select("li.name").first();
			author =first2!=null?first2.text():null;
		}else 	if (url.startsWith("http://blog.dwnews.com")){
			title =doc.select("title").text();
			if (title!=null)title=StringUtils.substringBefore(title, "_");
			date=DateUtils.matchDate(doc.select(".ep_time").text());
			author=doc.select(".author a").text();
			content=HtmlCleaner.getContentHtml(url,doc.select(".blogin .cotview"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select(".viewcot"));
			}
		}	else{
			title =doc.select(".newsview h1").text();
			if (StringUtils.isBlank(title)){
				title =doc.select(".title h1").text();
			}
			if (StringUtils.isBlank(title)){
				title =doc.select(" h1").text();
			}
			if (StringUtils.isBlank(title)){
				title =doc.select(" title").text();
			}
			if(StringUtils.isBlank(content))content=HtmlCleaner.getContentHtml(url,doc.select("#Zoom"));
			if(StringUtils.isBlank(content))content=HtmlCleaner.getContentHtml(url,doc.select(".viewcot"));
			if(date==null)date=DateUtils.matchDate(doc.select(".fa").text());
			if(date==null)date=DateUtils.matchDate(doc.select(".author").text());
			if(date==null)date=DateUtils.matchDate(doc.select(".ep_time").text());
			if(date==null)date=DateUtils.matchDate(doc.select(".ep_time_sx").text());
			if(date==null)date=DateUtils.matchDate(doc.select(".biaoti").text());
			if(date==null)date=DateUtils.matchDate(doc.select(".biaoti01").text());
			author=JSoupUtils.matchAuthor(doc, "作者：");
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
